<?php

Class admin_tiviController Extends baseController {

	public function index() { 
		$where = '1=1 ';
		$trangthai_search = (isset($_REQUEST['trangthai'])) ? $_REQUEST['trangthai'] : 'all';
		$idtivi_search = (isset($_REQUEST['tivi_id'])) ? $_REQUEST['tivi_id'] : 'all';
		$kichthuoc_search = (isset($_REQUEST['kichthuoc'])) ? $_REQUEST['kichthuoc'] : 'all';
		$giamin_search = (isset($_REQUEST['giamin'])) ? $_REQUEST['giamin'] : '';
		$giamax_search = (isset($_GET['giamax'])) ? $_GET['giamax'] : '';
		$soluongf_search = (isset($_REQUEST['soluongf'])) ? $_REQUEST['soluongf'] : '';
		$soluongt_search = (isset($_REQUEST['soluongt'])) ? $_REQUEST['soluongt'] : '';
		$sapxep = (isset($_REQUEST['sapxep'])) ? $_REQUEST['sapxep'] : 't.id';
		$by = (isset($_REQUEST['by'])) ? $_REQUEST['by'] : 'desc';
		
		if($trangthai_search != 'all') $where .= ' and t.TrangThai = '.$trangthai_search;
		if($idtivi_search != 'all') $where .= ' and t.id = '.$idtivi_search;
		if($kichthuoc_search != 'all') $where .= ' and KichThuoc = '.$kichthuoc_search;
		if($soluongf_search != '') $where .= ' and SoLuong >= '.$soluongf_search; 
		if($soluongt_search != '') $where .= ' and SoLuong <= '.$soluongt_search;
		if($giamin_search != '') $where .= ' and GiaBan >= '.str_replace('.','',$giamin_search);
		if($giamax_search != '') $where .= ' and GiaBan <= '.str_replace('.','',$giamax_search);
		$sapxep = 'CAST('.$sapxep.' AS UNSIGNED)';
		
		$tiviModel = new tivi();
		$current_page = (isset($_REQUEST['page'])) ? $_REQUEST['page'] : 1;
		$pos = ($current_page -1 ) * __PAGE_SIZE;
		$lstTv = $tiviModel->getTiviByWherePaging($where, $sapxep.' '.$by , $pos);
		$count = count((array)$tiviModel->getTiviByWhere($where, $sapxep.' '.$by));
		$pages = ceil($count/__PAGE_SIZE);

		$this->registry->template->lstTivi = $lstTv;
		$this->registry->template->pages = $pages;
		$this->registry->template->show('admin/tivi_view');
	}
	
	public function edit(){
		if(isset($_REQUEST['tivi_id'])){
			$tivi_id = $_REQUEST['tivi_id'];
			$tiviModel = new tivi();
			$tivi = $tiviModel->getTiviById($tivi_id);
			if(count($tivi)>0){
				$this->registry->template->tivi = $tivi;	
			}
		}
		$this->registry->template->show('admin/tivi_edit');	
	}
	
	public function save(){
		$tiviModel = new tivi();
		//Insert
		$insert = $_POST['insert']	;
		$name = trim($_POST['name'])	;
		$phangiai = $_POST['phangiai'];
		$kichthuoc = $_POST['kichthuoc']	;
		$giathat = $_POST['giathat'];
		$giaban = $_POST['giaban']	;
		$soluong = $_POST['soluong'];
		$hangsx = $_POST['hangsx']	;
		$loaitv = $_POST['loaitv'];
		$images = isset($_POST['images']) ? $_POST['images'] : '';
		$images2 = isset($_POST['images2']) ? $_POST['images2'] : '';
		$images3 = isset($_POST['images3']) ? $_POST['images3'] : '';
		$images4 = isset($_POST['images4']) ? $_POST['images4'] : '';
		$images5 = isset($_POST['images5']) ? $_POST['images5'] : '';
		
		$tinhnang = isset($_POST['tinhnang']) ? $_POST['tinhnang'] : ''; //echo '<pre>'; var_dump($tinhnang);die;
		$tinhnang = json_encode($tinhnang);
		$tinhnang = str_replace("\\","\\\\",$tinhnang); //echo $tinhnang;die; //echo '<pre>'; var_dump(json_decode($tinhnang));die;
		$mota = isset($_POST['mota']) ? $_POST['mota'] : '';
		$thongso = isset($_POST['thongso']) ? $_POST['thongso'] : '';
		
		
		if($insert == 'true')
		{
			$rs = $tiviModel->insert($name,$phangiai,$kichthuoc,$giathat,$giaban,$soluong,$images,$images2,$images3,$images4,$images5,$tinhnang,$mota,$thongso,$hangsx,$loaitv);
			if($rs) echo 'true';
			else echo 'false';
			die;
		}
		else
		{
			$tivi_id = $_POST['tivi_id'];
			$trangthai = $_POST['trangthai']; 
			//kiem tra neu hang dc chon co trang thai ko hoat dong (trangthai=0) thi tivi ko dc co trangthai=1
			$hangModel = new hangsanxuat();
			$hang = $hangModel->getHangSanXuatById($hangsx);
			if($hang->TrangThai == '0' && $trangthai=='1'){
				echo '0';
				die;
			}
			
			$rs = $tiviModel->update($name,$phangiai,$kichthuoc,$giathat,$giaban,$soluong,$images,$images2,$images3,$images4,$images5,$tinhnang,$mota,$thongso,$hangsx,$loaitv,$trangthai,$tivi_id);
			if($rs) echo 'true';
			else echo 'false';
			die;
		}
	}
	
	public function delete()
	{
		if(isset($_REQUEST['tivi_id']) && $_REQUEST['tivi_id']!='')	
		{
			$tivi_id = $_REQUEST['tivi_id'];
			$tiviModel = new tivi();
			$rs = $tiviModel->delete($tivi_id);
			header('location:'.__URL_BASE.'index.php?rt=admin_tivi');
			exit;
		}
	}
	
	public function ajaxImage()
	{
		//echo 'abc'; die;
		$path = "images/";
		
		$valid_formats = array("jpg", "png", "gif", "bmp");
		if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
		{
			$name = $_FILES['photoimg'.$_GET['image']]['name'];
			$size = $_FILES['photoimg'.$_GET['image']]['size'];
			//echo 'abc'; die;
			if(strlen($name))
				{
					list($txt, $ext) = explode(".", $name);
					if(in_array($ext,$valid_formats))
					{
						if($size<(1024*1024))
						{
							$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
							$tmp = $_FILES['photoimg'.$_GET['image']]['tmp_name'];
							if(move_uploaded_file($tmp, $path.$actual_image_name))
								{
								//mysql_query("UPDATE users SET profile_image='$actual_image_name' WHERE uid='$session_id'");

									echo "<img name='image".$_GET['image']."' src=' ".__URL_BASE.$path.$actual_image_name."' style='max-width:200px' />" ?>
									<input id="txtimg<?php echo $_GET['image'] ?>" type='hidden' name='image<?php echo $_GET['image'] ?>' value='<?php echo $actual_image_name?>' /><button id="btnremove<?php echo $_GET['image'] ?>" name="image<?php echo $_GET['image'] ?>"  style="position: absolute;margin-top: 89px;margin-left: 50px;" onclick="removeElement(this);" >Xóa ảnh</button>
							<?php	}
							else
								echo "failed";
						}
					else
						echo "Image file size max 1 MB";					
					}
				else
					echo "Invalid file format..";	
					}
				
			else
				echo "Please select image..!";
					
			exit;
		}		
	}

	function action_group(){
		$chk = $_POST['chkSelect']; 
		$request = $_POST['request']; // request link
		$tiviModel = new tivi();
		
		if(isset($_POST['active'])){
			foreach($chk as $k=>$v){
				$tiviModel->updateTrangThai('1',$k);
			}	
		}
		if(isset($_POST['deactive'])){
			foreach($chk as $k=>$v){
				$tiviModel->updateTrangThai('0',$k);
			}	
		}
		if(isset($_POST['delete'])){
			foreach($chk as $k=>$v){
				$tiviModel->delete($k);
			}	
		}
		
		header('location:'.__URL_BASE.'index.php'.$request);
	}
}

?>
